﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Accountability4net.Contact;
using System.Xml;

namespace Accountability4net.FileMonengineProvider
{
    public class FileName: BaseName
    {
        FileMonengineProvider provider_;
        FileContact contact_;
        protected FileName()
            : base()
        {
        }

        public FileName(FileMonengineProvider provider, FileContact contact)
            :this()
        {
            provider_ = provider;
            contact_ = contact;
        }

        //public static IName Parse(XmlNode node)
        //{
        //    return Parse(node, null);
        //}

        public IContact Contact
        {
            get
            {
                return contact_;
            }
        }

        public static IName Parse(XmlNode node, FileMonengineProvider provider, FileContact contact)
        {
            FileName name = new FileName(provider, contact);
            if (node != null)
            {
                name.FamilyName = node.Attributes["familyName"].Value;
                name.GivenName = node.Attributes["givenName"].Value;
                name.AdditionalNames = node.Attributes["additionalNames"].Value;
                name.NamePrefix = node.Attributes["namePrefix"].Value;
                name.NameSuffix = node.Attributes["nameSuffix"].Value;
                name.FormattedName = node.Attributes["formattedName"].Value;
            }
            return name;
        }

        public XmlNode Write(XmlNode element)
        {
            XmlNode node = provider_.Storage.SelectSingleNode(element, "x:name");
            if (node == null)
            {
                node = element.OwnerDocument.CreateElement("name", element.NamespaceURI);
                element.AppendChild(node);
            }
            XmlUtility.CreateAttribute(node, "familyName", FamilyName ?? String.Empty);
            XmlUtility.CreateAttribute(node, "givenName", GivenName ?? String.Empty);
            XmlUtility.CreateAttribute(node, "additionalNames", AdditionalNames ?? String.Empty);
            XmlUtility.CreateAttribute(node, "namePrefix", NamePrefix ?? String.Empty);
            XmlUtility.CreateAttribute(node, "nameSuffix", NameSuffix ?? String.Empty);
            XmlUtility.CreateAttribute(node, "formattedName", FormattedName ?? String.Empty);
            return node;
        }

    }
}
